package com.example.deeplearningjavafrombug.regex.lesson2;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Client {

    @Test
    public void test1() {
        List<String> data = new ArrayList<>();
        data.add("Title: The Mythical Man-Month, Author: Brooks");
        data.add("Title: Clean Code, Author: Martin");
        Map<String, String> map = new HashMap<>();
        for (String item : data) {
            String title = item.substring(item.indexOf("Title: ") + "Title: ".length(), item.indexOf(", "));
            String author = item.substring(item.indexOf("Author: ") + "Author: ".length());
            map.put(title, author);
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("entry: " + entry.getKey() + " -> " + entry.getValue());
        }
    }

    @Test
    public void test2() {
        List<String> data = new ArrayList<>();
        data.add("Title: The Mythical Man-Month, Author: Brooks");
        data.add("Error: Book not found, ErrorCode: 404");
        data.add("null");
        data.add("Title: Clean Code, Author: Martin");
        Map<String, String> map = new HashMap<>();
        for (String item : data) {
            if (item.matches("Title: .*, Author: .*")) {
                System.out.println("Find a valid string: " + item);
                String title = item.substring(item.indexOf("Title: ") + "Title: ".length(), item.indexOf(", "));
                String author = item.substring(item.indexOf("Author: ") + "Author: ".length());
                map.put(title, author);
            }
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("entry: " + entry.getKey() + " -> " + entry.getValue());
        }
    }

    @Test
    public void test3() {
        List<String> data = new ArrayList<>();
        data.add("Title: The Mythical Man-Month, Author: Brooks");
        data.add("Error: Book not found, ErrorCode: 404");
        data.add("null");
        data.add("Title: Clean Code, Author: Martin");
        Pattern pattern = Pattern.compile("Title: (.*), Author: (.*)");
        Map<String, String> map = new HashMap<>();
        for (String item : data) {
            Matcher matcher = pattern.matcher(item);
            if (matcher.matches()) {
                System.out.println("Find a valid string: " + item);
                map.put(matcher.group(1), matcher.group(2));
            }
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("entry: " + entry.getKey() + " -> " + entry.getValue());
        }
    }

    @Test
    public void test4() {
        List<String> data = new ArrayList<>();
        data.add("Title: The Mythical Man-Month, Author: Brooks");
        data.add("Error: Book not found, ErrorCode: 404");
        data.add("null");
        data.add("Title: Clean Code, Author: Martin");

        // 错误的做法，每次都会创建一个 Pattern 对象，效率低
        for (String item : data) {
            if (item.matches("Title: .*, Author: .*")) {
                System.out.println("Find a valid string: " + item);
            }
        }

        // 正确的做法，复用同一个 Pattern 对象，效率高
        Pattern pattern = Pattern.compile("Title: .*, Author: .*");
        for (String item : data) {
            if (pattern.matcher(item).matches()) {
                System.out.println("Find a valid string: " + item);
            }
        }
    }


}
